package cn.cdzdjd.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import cn.cdzdjd.dao.NoticeMapper;
import cn.cdzdjd.entity.Notice;
import cn.cdzdjd.service.NoticeService;
import cn.cdzdjd.util.UserHolder;

@Service
public class NoticeServiceImpl implements NoticeService {
	@Resource
	private NoticeMapper noticeMapper;

	@Override
	public List<String> queryNotice() {
		LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.gt(Notice::getShowEndtime, LocalDateTime.now()).and((qr) -> {
			if(UserHolder.getHolder() != null) {
				qr.eq(Notice::getCreateBy, UserHolder.getHolder()).or().eq(Notice::getType, "sys");
			}else {
				qr.eq(Notice::getType, "sys");
			}
			
		});
		List<Notice> list = noticeMapper.selectList(queryWrapper);
		List<String> notices = new ArrayList<>();
		list.forEach(item -> {
			notices.add(item.getContent());
		});
		return notices;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void insertNotice(Notice notice) {
		LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.gt(Notice::getShowEndtime, LocalDateTime.now()).eq(Notice::getCreateBy, UserHolder.getHolder());
		List<Notice> list = noticeMapper.selectList(queryWrapper);
		if (ObjectUtils.isEmpty(list)) {
			noticeMapper.insert(notice);
		}
	}

}
